<template>
    <div>
        <ZkModalVue :title="title" :size="'xs'" :visible="visible" @cancel="handleCancel" @ok="handleOk">
            <FunctionTree ref="functionTree" :roleId="id" @onChecked="onChecked"></FunctionTree>
        </ZkModalVue>
    </div>
</template>
<script>
import ZkModalVue from '@/appBase/ZkModal.vue';
import {  getRole, updateRole } from '@/api/role';
import FunctionTree from './components/FunctionTree.vue';
export default {
    name: "SelectFunction",
    components: {
        ZkModalVue,
        FunctionTree
    },
    data() {
        return {
            title: '功能权限',
            pageLoading: false,
            visible: false,
            /** 布局配置 */
            formLayout: {
                layout: 'horizontal',
                labelCol: {
                    xs: { span: 24 },
                    sm: { span: 5 }
                },
                wrapperCol: {
                    xs: { span: 24 },
                    sm: { span: 17 }
                }
            },
            id: null,
            info: {
                roleId: '',
                roleKey: '',
                roleName: '',
                roleSort: '',
                status: '',
                menuIds: []
            }
        }
    },
    methods: {
        show(param) {
            this.id = param.id;
            this._onOk = param.onOk;
            this._onCancel = param.onCancel;
            this.visible = true;
            this.$nextTick(() => {
                getRole(this.id).then((result) => {
                    Object.assign(this.info, result);
                    this.$refs.functionTree.refresh();
                });
            });
        },
        async handleOk() {
            const { roleId, roleKey, roleName, roleSort, status, menuIds } = this.info;
            if (this.id) {
                await updateRole({ roleId, roleKey, roleName, roleSort, status, menuIds:menuIds||[],menuCheckStrictly:false });
            }
            this.visible = false;
            if (this._onOk) this._onOk();
        },
        handleCancel() {
            this.visible = false;
            if (this._onCancel) this._onCancel();
        },
        onChecked(checkedKeys) {
            this.info.menuIds = checkedKeys;
        }
    }
}
</script>